﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace Slex.Lib.Interactions
{
    /// <summary>
    /// A helper class for registering dependency properties, and to fire notifications
    /// </summary>
    public static class DependencyPropertyHelper
    {
        /// <summary>
        /// Listen for change of a property of the source, and feedback the same to the context via the callback
        /// </summary>
        /// <param name="propertyPath"></param>
        /// <param name="element"></param>
        public static System.Windows.DependencyProperty RegisterForNotification
            (this DependencyObject owner, object source,
            string propertyPath, PropertyChangedCallback callback)
        {

            return RegisterForNotification(owner, source, propertyPath, callback, false);

        }


        /// <summary>
        /// Listen for change of a property of the source, and feedback the same to the context via the callback.
        /// Creates a two way binding if you
        /// </summary>
        /// <param name="propertyPath"></param>
        /// <param name="element"></param>
        public static System.Windows.DependencyProperty RegisterForNotification
            (this DependencyObject owner, object source,
            string propertyPath, PropertyChangedCallback callback, bool twoWay)
        {

            //Bind to a depedency property
            Binding b = new Binding(propertyPath) { Source = source, Mode = BindingMode.OneWay };
            if (twoWay) b.Mode = BindingMode.TwoWay;

            var prop = System.Windows.DependencyProperty.RegisterAttached(
                "Listener" + propertyPath + DateTime.Now.Ticks,
                typeof(object),
                owner.GetType(),
                new System.Windows.PropertyMetadata(callback));

            BindingOperations.SetBinding(owner, prop, b);

            return prop;

        }
    }
}
